<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-03-10
 * Time: 22:51
 */

namespace app\ops\controller;

use app\exception\CustomException;
use app\model\MoneyLog;
use app\tools\MIniprogram;
use think\Db;
use think\Exception;

class Repair extends Base

{

    function index()
    {
        $list = db('repair_person r')
            ->join('auth_data d', 'd.rp_id = r.id')
            ->field('r.wx_number, r.fullname as name, r.mobile, r.service_type, r.status, r.id, r.address, r.service_type, r.thaw_time, r.remark, r.avatar, r.customer_id')
            ->order('r.id desc')
            ->paginate(15)->each(function (&$item){
                $service = db('service_config')->where(['id'=> ['in', explode(',', $item['service_type'])]])->column('name');
                $item['service'] = implode(',', $service);
                if(strtotime($item['thaw_time']) <= time() && $item['status'] == 5)
                {
                    $item['status'] = 3;
                }
                return $item;
            });

        $this->assign('list', $list);
        return $this->fetch();
    }

    function detail($id = 0)
    {
        $list = db('repair_person r')
            ->join('auth_data d', 'd.rp_id = r.id')
            ->where(['r.id'=> $id])
            ->field('r.customer_id, r.fullname as name, r.wx_number, r.mobile, r.service_type, r.status, r.id, r.address, r.lat, r.lng, r.distance, r.operater, r.reason, r.operate_time, d.idcard, d.idcard_face, d.idcard_back, r.thaw_time')
            ->find();

        $service = db('service_config')->where(['id'=> ['in', explode(',', $list['service_type'])]])->column('name');
        $diam = db('customer')->where(['id'=> $list['customer_id']])->value('diam');
        $list['diam'] = $diam;
        if(strtotime($list['thaw_time']) <= time() && $list['status'] == 5)
        {
            $list['status'] = 3;
        }
        $this->assign('data', $list);
        $this->assign('service', implode(',', $service));
        return $this->fetch();
    }

    function setStatus()
    {
        $post = request()->post();
        $status = db('repair_person')->where(['id'=> $post['id']])->field('customer_id, status')->find();
        if($status['status'] == 2 || $status['status'] == 4)
        {
            if($post['status'] == 3)
            {
                Db::startTrans();
                try{
                    db('repair_person')->update($post);
                    db('customer')->where(['id'=> $status['customer_id']])->setInc('diam', 500);
                    MoneyLog::addLog([
                        'u_id'=> $status['customer_id'],
                        'type'=> 3,
                        'value'=> 500,
                        'reason_type'=> 10,
                        'child_id'=> 0,
                    ]);
                    Db::commit();
                }catch (Exception $exception){
                    Db::rollback();
                    throw new CustomException();
                }
            }else{
                db('repair_person')->update($post);
            }
        }

        $form = db('form_data')->where(['customer_id'=> $status['customer_id']])->field('form_id, openid, id')->order('create_time desc')->find();
        MIniprogram::sendTemplate([
            'openid'=> $form['openid'],
            'template_id'=> 'MFUizOdcN95F5qa9N7hkWVY3q6RKtK_qxs8JkFqbPHs',
            'form_id'=> $form['form_id'],
            'page'=> 'pages/mine/mine',
            'data'=> [
                'keyword1'=> ['value'=> '维修师傅认证'],
                'keyword2'=> ['value'=> $post['status'] == 3?"通过认证":"认证失败"],
                'keyword3'=> ['value'=> isset($post['reason'])?$post['reason']:''],
                'keyword4'=> ['value'=> date('Y-m-d H:i:s')],
            ]
        ]);
        db('form_data')->where(['id'=> $form['id']])->delete();


        return success([]);
    }

    function dongjie()
    {
        $post = request()->post();
        $data = [
            'status'=> 5,
            'id'=> $post['id'],
            'thaw_time'=> date('Y-m-d H:i:s', time() + 86400*intval($post['day']))
        ];
        db('repair_person')->update($data);

        return success([]);
    }

    function addDiam()
    {
        $post = request()->post();
        db('customer')->where(['id'=> $post['id']])->setInc('diam', $post['diam']);
        MoneyLog::addLog([
            'u_id'=> $post['id'],
            'type'=> 3,
            'value'=> $post['diam'],
            'reason_type'=> 11,
            'child_id'=> 0,
        ]);

        return success([]);
    }

    function edit()
    {
        $param = request()->post();
        if($param)
        {
            unset($param['image']);
            db('repair_person')->update($param);
            return success([]);
        }else{
            $id = request()->get('id');
            $data = db('repair_person')->where(['id'=> $id])->field('remark, id, avatar')->find();
            $this->assign('data', $data);
            return $this->fetch();
        }
    }
}